/*
 * Copyright (C) 2001-2005 Pleasant nightmare studio
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.pleasantnightmare.noraxidium;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import org.newdawn.slick.SlickException;
import org.pleasantnightmare.noraxidium.util.CrashExceptionHandler;
import org.pleasantnightmare.noraxidium.util.ParamProcessor;

/**
 * Runner class.
 * Sets up basic facilities as logging, parses configuration
 * parameters, configures main game class and starts it.
 *
 * @author deus
 * @version 1.0
 * @since Jul 6, 2010 2:21:48 PM
 */
public class Noraxidium {
  private static final Logger LOGGER = Logger.getLogger(Noraxidium.class);
  private CrashExceptionHandler crashExceptionHandler;
  private NoraxidiumGameContainer gameContainer;
  private NoraxidiumGame game;

  public Noraxidium() throws SlickException {
    game = new NoraxidiumGame();
    gameContainer = new NoraxidiumGameContainer(game);
  }

  private void setupBasicFacilities() {
    // Game specific settings are not considered basic facilities
    crashExceptionHandler = new CrashExceptionHandler();
    Thread.currentThread().setUncaughtExceptionHandler(crashExceptionHandler);

    DOMConfigurator.configure(
        ClassLoader.getSystemResource("etc/logging_config.xml")
    );

    LOGGER.debug("Basic facilities setup done.");
  }

  private void configureApplication(String[] args) throws IllegalAccessException {
    // Parse parameters here, and use mutators to configure 'game' instance
    ParamProcessor.configure(crashExceptionHandler, args);
    ParamProcessor.configure(gameContainer, args);

    gameContainer.initialize();

    LOGGER.debug("Application configured.");
  }

  private void start() throws SlickException {
    LOGGER.info("Starting game...");
    gameContainer.start();
    LOGGER.info("Quitting.");
  }

  public static void main(String[] args) throws IllegalAccessException, SlickException {
    Noraxidium theCyberpunkProject = new Noraxidium();
    theCyberpunkProject.setupBasicFacilities();
    theCyberpunkProject.configureApplication(args);
    theCyberpunkProject.start();
  }
}
